package com.css.fxfzaqbz.modules.data.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.HospitalData;
import com.css.fxfzaqbz.modules.data.queryParams.HospitalDataQueryParams;
import com.css.fxfzaqbz.modules.data.repository.HospitalDataRepository;
import com.css.fxfzaqbz.modules.data.service.HospitalDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class HospitalDataServiceImpl implements HospitalDataService {
    @Resource
    HospitalDataRepository hospitalDataRepository;

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public HospitalData updateHospitalData(ActiveFaultData activeFaultData) {
//
//        activeFaultData.setAfdUpdatetime(PlatformDateUtils.getCurrentTimestamp());
//
//        return activeFaultDataRepository.updateActiveFaultData(activeFaultData);
//    }

    @Override
    @Transactional
    public HospitalData saveHospitalData(HospitalData hospitalData) {
        hospitalData.setHdId(UUIDGenerator.getUUID());
        hospitalData.setDelFlag(YNEnum.Y.toString());
        hospitalData.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        return hospitalDataRepository.saveHospitalData(hospitalData);
    }

    @Override
    public Map<String,Object> getAllHospitalData(HospitalDataQueryParams queryParams) {
        return hospitalDataRepository.getAllHospitalDataParams(queryParams);
    }

    @Override
    public HospitalData getHospitalDataById(String id) {

        return hospitalDataRepository.getHospitalDataById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHospitalDatas(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        hospitalDataRepository.deleteHospitalDataById(ids,timestamp);
    }

    @Override
    public List<HospitalData> getAll() {

        return hospitalDataRepository.getAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws IOException {
        List<HospitalData> hospitalData = FileUtil2.importExcel(file, HospitalData.class);
        for(int i = 0 ; i<hospitalData.size();i++){
            hospitalData.get(i).setHdId(UUIDGenerator.getUUID());
            hospitalData.get(i).setDelFlag(YNEnum.Y.toString());
            hospitalData.get(i).setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        }
        hospitalDataRepository.importExcel(hospitalData);
    }
}
